import NProgress from 'nprogress'
import { MessageBox } from 'element-ui'
import router from './index'
import store from '../store'

router.beforeEach((to, from, next) => {
  NProgress.start()
  const {
    title
  } = to.meta
  if (title) {
    document.title = title
  }
  let sessionId = localStorage.getItem('sessionId')
  let userState = localStorage.getItem('userState')
  if (to.meta.requiresAuth) {
    if (sessionId) {
      if (userState === '98' && to.meta.title !== '个人认证') {
        MessageBox.alert('请重新登录,进行实名认证', '账号已经更换，此账号还未实名认证', {
          confirmButtonText: '重新登录',
          type: 'warning',
          showClose: false,
          center: true
        })
          .then(() => {
            store.dispatch('logout')
            next({
              path: '/login'
            })
          })
      } else {
        next()
      }
    } else {
      next({
        path: '/login'
      })
    }
  } else {
    next()
  }
})

router.afterEach(() => {
  NProgress.done()
})
